# @Author   : 懒羊羊
# @File     : ecshop_login.py
# @Time     : 2024/12/24 11:06
import requests
from common.config import data_path

lg_url = "http://192.168.88.102/user.php"

lg_data = {
    "username": "lbxx",
    "password": "123456",
    "back_act": "http://192.168.88.102/",
    "act": "act_login"
}

lg_r = requests.post(lg_url, data=lg_data)

# print(lg_r.text)

# set_cookie = lg_r.headers.get("Set-Cookie")
# print(lg_r.headers)
# print(lg_r.cookies)
# print(set_cookie)

# 新增留言  第一种使用登录的lg_r.cookies 传给cookies
# xzly_url = "http://192.168.88.102/user.php"
#
# ly_data = {
#     "msg_type": "3",
#     "msg_title": "jk008",
#     "msg_content": "78999",
#     "act": "act_add_message"
# }
#
# fdata = {"message_img":
#          ("1.txt", open(r"D:\python-workspace\python-lizhe\python-base-trl\apiAuto\datas\1.txt", "rb"), "text/plain")}
#
# # h = {"Content-Type": "multipart/form-data"}
#
# xzly_r = requests.post(xzly_url, data=ly_data, files=fdata, cookies=lg_r.cookies)
#
# print(xzly_r.text)


# 新增留言  第二种将cookies 提取出来给对应接口的header头中
xzly_url = "http://192.168.88.102/user.php"

# 提取cookie
set_cookie = lg_r.cookies
print(set_cookie)

print(set_cookie.items())

s = ""
for i in set_cookie.items():
    key1, value1 = i
    s += f"{key1}={value1};"

# 将s 添加到header的cookie中
h = {"cookie": s}

# 普通数据
ly_data = {
    "msg_type": "3",
    "msg_title": "jk008",
    "msg_content": "78999",
    "act": "act_add_message"
}

# 文件数据
fdata = {"message_img":
             ("1.txt", open(r"D:\python-workspace\python-lizhe\python-base-trl\apiAuto\datas\1.txt", "rb"),
              "text/plain")}

xzly_r = requests.post(xzly_url, data=ly_data, files=fdata, headers=h)

print(xzly_r.text)
